package com.family.tools;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 封装的日期计算工具类
 */
public class DateFormatUtils {

    public static String getCurDate()
    {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String now = format.format(new Date());
        return now;
    }

    public static String getNowTime()
    {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String now = format.format(new Date());
        return now;
    }

    public static String changeDate(String date)
    {
        SimpleDateFormat sdf =   new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat sd2 =   new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date od =sdf.parse(date);

            return sd2.format(od);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return "";
    }

    public static String changeTime(String date,String time)
    {
        SimpleDateFormat sdf =   new SimpleDateFormat("yyyyMMddHHmmss");
        SimpleDateFormat sd2 =   new SimpleDateFormat("HH:mm:ss");
        try {
            Date od =sdf.parse(date+time);

            return sd2.format(od);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return "";
    }

    public static String getCurTime()
    {
        SimpleDateFormat format = new SimpleDateFormat("HHmmss");
        String now = format.format(new Date());
        return now;
    }

    public static String getJSTime()
    {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        String now = format.format(new Date());
        return now;
    }

    public static String getCurDateTime()
    {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmm");
        String now = format.format(new Date());
        return now;
    }

    public static String parseDate(String date)
    {
        SimpleDateFormat sdf =   new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sd2 =   new SimpleDateFormat("yyyyMMdd");
        try {
            Date od =sdf.parse(date);

            return sd2.format(od);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return "";
    }

    /*
    获取时间差
     */
    public static String getTimesToNow(String date){
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmm");
        String now = format.format(new Date());
        String returnText = null;
        try {
            long from = format.parse(date).getTime();
            long to = format.parse(now).getTime();
            int days = (int) ((to - from)/(1000 * 60 * 60 * 24));
            if(days == 0){//一天以内，以分钟或者小时显示
                int hours = (int) ((to - from)/(1000 * 60 * 60));
                int minutes = (int) ((to - from)/(1000 * 60));
                if(hours == 0){

                    if(minutes == 0){
                        returnText = "刚刚";
                    }else{
                        returnText = minutes + "分钟";
                    }
                }else{
                    returnText = hours + "小时 "+minutes%60+"分钟";
                }
            } else if(days == 1){
                returnText = "昨天";
            }else{
                returnText = days + "天前";
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return returnText;
    }

    public static  int DateCompare(String to)
    {

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        Date now = new Date();
        Date dto = null;
        try {
            dto = dateFormat.parse(to);

        } catch (ParseException e) {
            e.printStackTrace();
        }

        return now.compareTo(dto);
    }

//    public static void main(String args[])
//    {
//    	int flag = DateCompare("20170301");
//    	if(flag<0)
//    	{
//    		System.out.println("未到期");
//    	}else
//    	{
//    		System.out.println("已到期");
//    	}
//    }
}
